// pages/my-driver/add-driver.js
import util from '../../utils/util'
import Dialog from 'vant-weapp/dialog/dialog';
const app = getApp();
const http = app.http;
const userInfoManager = app.userInfoManager;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    color: '#000000',
    background: '#ffffff',
    title: "",
    isDisable: false,
    isUpdate:false,
    showOverlay:false,
    showOverlayPic:"",
    isHead:true,
    driverData: {
      name: "",
      age: "",
      sex: "",
      birthday: "",
      headPic: "../../images/icon_camera.png",
      healthCertificatePics: "../../images/icon_camera.png",
      supplierId: "",
      phone: ""
    },
    isNo: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let titles = "";
    let isDisables = false;
    console.log(options.ids);
    if (options.isAdd == "true") {
      titles = "添加";
      isDisables = false;
    } else {
      titles = "详情";
      isDisables = true;
    }
    this.setData({
      title: titles,
      isDisable: isDisables
    });
    if (options.ids && options.supplierId) {
      http.driverList({
        id: options.ids,
        supplierId: options.supplierId
      }).then(res => {
        if (res.code == 0) {
          if(res.data.length >0){
            var now = new Date();
            var year = now.getFullYear();
            res.data[0].age = year- parseInt(res.data[0].birthday.split("-")[0]);
            this.setData({
              driverData: res.data[0]
            });
            console.log(this.data.driverData);
          }
        } else {
          wx.showToast({
            duration: 1000,
            icon: 'none',
            title: res.msg
          })
        }
      })
    }
  },
  /**点击照片 */
  clickUpdatePic(e){
    console.log(e);
    if (!this.data.isDisable) return;
    let photoUrl = e.currentTarget.dataset.item;
    let isHeads = e.currentTarget.dataset.index;
    this.setData({ showOverlay: true, showOverlayPic: photoUrl, isHead: isHeads === "head" ? true:false});
  },
  onClickHid(e) {
    this.setData({ showOverlay: false});
  },
  onClickHide(e){
    this.setData({ showOverlay: false, isUpdate: true});
  },
  /**照片上传 */
  afterReadHead(event) {
    const {
      file,
      name
    } = event.detail;
    http.uploadFile(file.path, {
      isWebPage: "0",
      isTransCoding: "0"
    }).then(res => {
      if (res.code == 0) {
        console.log(res.data.objectAccessUrl);
        this.setData({
          ["driverData.headPic"]: res.data.objectAccessUrl
        });
      } else {
        wx.showToast({
          duration: 1000,
          icon: 'none',
          title: res.msg
        })
      }
    })
  },
  /**健康证上传 */
  afterReadHealth(event) {
    const {
      file,
      name
    } = event.detail;
    http.uploadFile(file.path, {
      isWebPage: "0",
      isTransCoding: "0"
    }).then(res => {
      if (res.code == 0) {
        console.log(res.data.objectAccessUrl);
        this.setData({
          ["driverData.healthCertificatePics"]: res.data.objectAccessUrl
        });
      } else {
        wx.showToast({
          duration: 1000,
          icon: 'none',
          title: res.msg
        })
      }
    })

  },
  /**更新司机 */
  updateDriver(e){
    if (this.data.driverData.name === "") {
      wx.showToast({
        duration: 1000,
        icon: 'none',
        title: "请输入司机姓名"
      })
      return
    }
    if (this.data.driverData.phone === "") {
      wx.showToast({
        duration: 1000,
        icon: 'none',
        title: "请输入司机电话号码"
      })
      return
    }
    if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(this.data.driverData.phone))) {
      wx.showToast({
        duration: 1000,
        icon: 'none',
        title: "司机电话号码有误，请重新填写"
      });
      this.setData({
        ["driverData.phone"]: ""
      });
      return
    }
    if (this.data.driverData.sex === "") {
      wx.showToast({
        duration: 1000,
        icon: 'none',
        title: "请输入司机性别"
      })
      return
    }
    if (this.data.driverData.age === "") {
      wx.showToast({
        duration: 1000,
        icon: 'none',
        title: "请输入司机年龄"
      })
      return
    }
    if (this.data.driverData.headPic === "" || this.data.driverData.headPic === "../../images/icon_camera.png") {
      wx.showToast({
        duration: 1000,
        icon: 'none',
        title: "请上传司机照片"
      })
      return
    }
    if (this.data.driverData.healthCertificatePics === "" || this.data.driverData.healthCertificatePics === "../../images/icon_camera.png") {
      wx.showToast({
        duration: 1000,
        icon: 'none',
        title: "请上传司机健康证"
      })
      return
    }
    var now = new Date();
    var year = now.getFullYear();
    this.data.driverData.birthday = year - parseInt(this.data.driverData.age) + "-01-01 00:00:00";
    let personInfo = userInfoManager.getSupplierInfo();
    this.data.driverData.supplierId = personInfo.id;
    this.data.driverData.isDelete = null;
    http.updateDriver(this.data.driverData).then(res => {
      if (res.code == 0) {
        wx.showToast({
          duration: 1000,
          icon: 'none',
          title: "修改成功"
        });
        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          })
        }, 1000);
      } else {
        wx.showToast({
          duration: 1000,
          icon: 'none',
          title: res.msg
        })
      }
    })
  },
  /**删除司机 */
  deleteDriver(e){
    Dialog.confirm({
      title: '删除确认',
      message: "是否删除该司机信息",
      confirmButtonColor: "#21af65",
    }).then(() => {
      http.driverDelete(this.data.driverData.id).then(res => {
        if (res.code == 0) {
          wx.showToast({
            duration: 1000,
            icon: 'none',
            title: '删除成功'
          })
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            })
          }, 1000);
        } else {
          wx.showToast({
            duration: 1000,
            icon: 'none',
            title: res.msg
          })
        }
      })
    }).catch(() => {

    });
  },
  /**确认添加 */
  addDriver(e) {
    console.log(this.data.driverData);
    if (this.data.driverData.name === "") {
      wx.showToast({
        duration: 1000,
        icon: 'none',
        title: "请输入司机姓名"
      })
      return
    }
    if (this.data.driverData.phone === "") {
      wx.showToast({
        duration: 1000,
        icon: 'none',
        title: "请输入司机电话号码"
      })
      return
    }
    if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(this.data.driverData.phone))) {
      wx.showToast({
        duration: 1000,
        icon: 'none',
        title: "司机电话号码有误，请重新填写"
      });
      this.setData({
        ["driverData.phone"]: ""
      });
      return
    } 
    if (this.data.driverData.sex === "") {
      wx.showToast({
        duration: 1000,
        icon: 'none',
        title: "请输入司机性别"
      })
      return
    }
    if (this.data.driverData.age === "") {
      wx.showToast({
        duration: 1000,
        icon: 'none',
        title: "请输入司机年龄"
      })
      return
    }
    if (this.data.driverData.headPic === "" || this.data.driverData.headPic === "../../images/icon_camera.png") {
      wx.showToast({
        duration: 1000,
        icon: 'none',
        title: "请上传司机照片"
      })
      return
    }
    if (this.data.driverData.healthCertificatePics === "" || this.data.driverData.healthCertificatePics === "../../images/icon_camera.png") {
      wx.showToast({
        duration: 1000,
        icon: 'none',
        title: "请上传司机健康证"
      })
      return
    }
    var now = new Date();
    var year = now.getFullYear();
    this.data.driverData.birthday = year - parseInt(this.data.driverData.age) + "-01-01 00:00:00";
    let personInfo = userInfoManager.getSupplierInfo();
    this.data.driverData.supplierId = personInfo.id;
    http.addDriver(this.data.driverData).then(res => {
      if (res.code == 0) {
        wx.showToast({
          duration: 1000,
          icon: 'none',
          title: "提交成功"
        });
        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          })
        }, 1000);
      } else {
        wx.showToast({
          duration: 1000,
          icon: 'none',
          title: res.msg
        })
      }
    })
  },
  onNameInputListener(e) {
    let phoneValue = util.validateNull(e.detail);
    if(this.data.isDisable){
      this.setData({
        isUpdate: true
      })
    }
    this.setData({
      ["driverData.name"]: phoneValue
    })
  },
  onSexInputListener(e) {
    let phoneValue = util.validateNull(e.detail);
    if (this.data.isDisable) {
      this.setData({
        isUpdate: true
      })
    }
    this.setData({
      ["driverData.sex"]: phoneValue
    })
  },
  onPhoneInputListener(e) {
    let phoneValue = util.validateNumber(e.detail);
    if (this.data.isDisable) {
      this.setData({
        isUpdate: true
      })
    }
    this.setData({
      ["driverData.phone"]: phoneValue
    })
  },
  onAgeInputListener(e) {
    let ageValue = util.validateNumber(e.detail);
    if (this.data.isDisable) {
      this.setData({
        isUpdate: true
      })
    }
    this.setData({
      ["driverData.age"]: ageValue
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})